package com.songtai.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.songtai.dao.ImageDao;
import com.songtai.po.PhotoObj;
import com.songtai.util.Pager;

/**
 * @author 赵中杨
 * @email zhaozhongyang717@gmail.com
 * @date 2010 2010-4-20 下午03:19:08
 */
public class ImageDaoImpl implements ImageDao {
	private SessionFactory sessionFactory;

	@Override
	public void addImage(PhotoObj obj) {
		this.sessionFactory.getCurrentSession().persist(obj);
	}

	@Override
	public int deleteImage(String pid) {
		return sessionFactory.getCurrentSession().createQuery("delete from PhotoObj p where p.id in('" + pid + "')").executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<PhotoObj> getImage(Long nodeid, int currentPage, int pageSize) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from PhotoObj p where p.nid=:nodeid order by p.insertDate desc").setLong("nodeid", nodeid).list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<PhotoObj> list = sesion.createQuery("from PhotoObj p where p.nid=:nodeid order by p.insertDate desc").setLong("nodeid", nodeid).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<PhotoObj> pager = new Pager<PhotoObj>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PhotoObj> getRecentImage(int pageSize, String type,Long nodeid) {
		return sessionFactory.getCurrentSession().createQuery("from PhotoObj p where p.type=:type and p.nid=:nodeid and p.name is not null and p.name!='' order by p.insertDate desc").setString("type", type).setLong("nodeid", nodeid).setMaxResults(pageSize).list();
	}

	@SuppressWarnings("unchecked")
	public Pager<PhotoObj> getAllImage(int currentPage, int pageSize, String type) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from PhotoObj p where p.type=:type order by p.insertDate desc").setString("type", type).list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<PhotoObj> list = sesion.createQuery("from PhotoObj p where p.type=:type order by p.insertDate desc").setString("type", type).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<PhotoObj> pager = new Pager<PhotoObj>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<PhotoObj> getAllImage(int currentPage, int pageSize, String type, Long nodeid) {
		String orderType = " p.insertDate";
		if(PhotoObj.TYPE_DESIGNER.equals(type)){
			orderType = " p.name";
		}
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from PhotoObj p where p.nid=:nodeid and p.type=:type order by "+orderType+" desc").setLong("nodeid", nodeid).setString("type", type).list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<PhotoObj> list = sesion.createQuery("from PhotoObj p where p.nid=:nodeid and p.type=:type order by "+orderType+" desc").setLong("nodeid", nodeid).setString("type", type).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<PhotoObj> pager = new Pager<PhotoObj>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<PhotoObj> getAllImageGroupByBiaoqian(int currentPage, int pageSize, String type, Long nodeid) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from PhotoObj p where p.nid=:nodeid and p.type=:type group by p.reMark").setLong("nodeid", nodeid).setString("type", type).list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<PhotoObj> list = sesion.createQuery("from PhotoObj p where p.nid=:nodeid and p.type=:type group by p.reMark order by p.insertDate desc").setLong("nodeid", nodeid).setString("type", type).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<PhotoObj> pager = new Pager<PhotoObj>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	/**
	 * 搜索
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pager<PhotoObj> searchImage(int currentPage, int pageSize, String type, String keyword) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from PhotoObj p where p.title like ?  and p.type=:type order by p.insertDate desc").setString(0, "%" + keyword + "%").setString("type", type).list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<PhotoObj> list = sesion.createQuery("from PhotoObj p where p.title like ? and p.type=:type order by p.insertDate desc").setString(0, "%" + keyword + "%").setString("type", type).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<PhotoObj> pager = new Pager<PhotoObj>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	@Override
	public PhotoObj getImageById(String pid) {
		return (PhotoObj) sessionFactory.getCurrentSession().get(PhotoObj.class, pid);
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void updateImage(PhotoObj obj) {
		sessionFactory.getCurrentSession().update(obj);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PhotoObj> getImageByNodeid(Long nodeid, int pageSize, String type) {
		return sessionFactory.getCurrentSession().createQuery("from PhotoObj p where p.nid=:nodeid and p.type=:type order by p.insertDate desc").setLong("nodeid", nodeid).setString("type", type).setMaxResults(pageSize).list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PhotoObj> getImagesByBiaoqian(String type, String biaoqian, Long nodeid) {
		return sessionFactory.getCurrentSession().createQuery("from PhotoObj p where p.nid=:nodeid and p.type=:type and p.reMark=:reMark order by p.insertDate desc").setLong("nodeid", nodeid).setString("type", type).setString("reMark", biaoqian).list();
	}
}
